<?php

declare(strict_types=1);

namespace App\Application\Admin\Controller;
use App\Annotation\Api;
use App\Annotation\View;
use App\Application\Admin\Controller\RequestParam\RoomlabelSubmitRequestParam;
use App\Application\Admin\Middleware\AdminMiddleware;
use App\Application\Admin\Model\Roomlabel;
use App\Application\Admin\Service\RoomlabelService;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\RequestMapping;


#[Middleware(AdminMiddleware::class)]
#[Controller("admin/roomlabel")]
class RoomlabelController extends AbstractController
{
    #[View]
    #[GetMapping("index")]
    public function index()
    {

    }

    #[Api]
    #[GetMapping("index/lists")]
    function lists()
    {
        $where = [];
        $name = $this->request->input('name', '');
        if ($name != '') {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }
        $lists = Roomlabel::where($where)
            ->orderBy('room_label_id', 'DESC')
            ->paginate();
        return compact('lists');
    }

    #[Api]
    #[RequestMapping("edit", ["POST", "PUT"])]
    function submitEdit()
    {
        $request_param = new RoomlabelSubmitRequestParam();
        $request_param->validatedThrowMessage();
        $room_label_id = $request_param->getRoomLabelId();
        $res = Roomlabel::createStore($room_label_id, [
             'name' => $request_param->getName()
        ]);
        RoomlabelService::getInstance()
            ->updateroomlabel($role->role_id);
        return $res ? $this->returnSuccessJson(compact('res')) : $this->returnErrorJson();
    }

    #[View]
    #[GetMapping("edit")]
    function edit()
    {
        $room_label_id = (int)$this->request->input('room_label_id', 0);
        return ['title' => $room_label_id > 0 ? '编辑标签' : '新增标签'];
    }


    #[Api]
    #[GetMapping("edit/{room_label_id}")]
    function editInfo(int $room_label_id)
    {
        $room_label = Roomlabel::where('room_label_id', $room_label_id)->select('*')
            ->first() ?: [];
        return compact('room_label');
    }


}
